package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录检查过滤器
 * 
 * @author 崔诗新
 *
 */
@WebFilter("/*")
public class Filter2_LoginCheckFilter implements Filter {

	public Filter2_LoginCheckFilter() {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		String path = req.getServletPath();
		if (!path.substring(1, 4).equals("css")&& !path.substring(1, 3).equals("js") 
				&& !path.substring(1, 4).equals("img") && !path.substring(1, 8).equals("laydate") 
				&& !path.substring(1).equals("LoginServlet") && !path.substring(1).equals("login.html")
				&& !path.substring(1,6).equals("admin")) {
			if (req.getSession().getAttribute("user") == null && req.getSession().getAttribute("admin") == null)
				resp.sendRedirect(req.getContextPath() + "/login.html");
		}
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
